package test.my.com.myapplicationtest.retrofit_net;

import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;
import test.my.com.myapplicationtest.utils.GsonUtil;

/**
 * Author : Cooper
 * Time : 2017/8/21  17:02
 * Description : 接口调用工具类
 */

public class HttpLoader {
    private static ApiManager mApiManager;

    private HttpLoader() {
        mApiManager = RetrofitManager.getInstance().create(ApiManager.class);
    }

    private static class HttpLoaderHolder {
        private static final HttpLoader INSTANCE = new HttpLoader();
    }

    public static HttpLoader getInstence() {
        return HttpLoaderHolder.INSTANCE;
    }
    /**
     * 创建JsonRPC请求参数
     * @param method
     * @param params
     * @return
     */
    private RequestEntity createRequest(String method, HashMap<String, Object> params) {
        RequestEntity requestEntity = new RequestEntity();
        requestEntity.setId(UUID.randomUUID().hashCode());
        requestEntity.setJsonrpc("2.0");
        requestEntity.setMethod(method);
        List<HashMap<String, Object>> list = new ArrayList<>();
        list.add(params);
        requestEntity.setParams(list);
        return requestEntity;
    }
    /**
     * 创建JsonRPC请求参数
     * @param method
     * @return
     */
    private String createRequest(String method) {
        RequestEntity requestEntity = new RequestEntity();
        requestEntity.setId(UUID.randomUUID().hashCode());
        requestEntity.setJsonrpc("2.0");
        requestEntity.setMethod(method);
        List<HashMap<String, Object>> list = new ArrayList<>();
        requestEntity.setParams(list);
        return GsonUtil.gsonString(requestEntity);
    }

    /**
     * 请求接口统一调用
     * @param observable
     * @param compositeSubscription
     * @param func1
     * @param subscriberCallBack
     */
    private void normalPost(Observable observable, CompositeSubscription compositeSubscription, Func1 func1, SubscriberCallBack subscriberCallBack) {
        compositeSubscription.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(func1)
                .subscribe(subscriberCallBack)
        );
    }

    public void getEarning(String method, CompositeSubscription compositeSubscription, SubscriberCallBack<EarningResponseEntity> subscriber) {
        normalPost(mApiManager.postActivity(createRequest(method)),compositeSubscription,new Func1<String, ResponseEntity<EarningResponseEntity>>() {//将接口返回的String数据，转换为实体类
            @Override
            public ResponseEntity<EarningResponseEntity> call(String s) {
                ResponseEntity<EarningResponseEntity> response = GsonUtil.gsonToResponse(s,new TypeToken<ResponseEntity<EarningResponseEntity>>() {}.getType());
                return response;
            }
        },subscriber);
    }

}
